home *** CD-ROM | disk | FTP | other *** search
- /* Creates and initializes a new list node.
- Copyright (C) 1989 Free Software Foundation, Inc.
- written by Douglas C. Schmidt (schmidt@ics.uci.edu)
-
- This file is part of GNU GPERF.
-
- GNU GPERF is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 1, or (at your option)
- any later version.
-
- GNU GPERF is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with GNU GPERF; see the file COPYING. If not, write to
- the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
-
- #include <stdio.h>
-
- #include "options.h"
- #include "stderr.h"
- #include "xmalloc.h"
-
- #include "listnode.h"
-
- /* See comments in perfect.cc. */
- extern int occurrences[ALPHABET_SIZE];
-
- /*******************************************************************************\
- * Prototypes *
- \*******************************************************************************/
-
- static void set_sort( char * base, int len );
-
- /***********************************************************************\
- * *
- * name: set_sort *
- * *
- * descr: Sorts the key set alphabetically to speed up subsequent *
- * operations. Use insertion sort since the set is probably *
- * very small. *
- * *
- \***********************************************************************/
-
- static void set_sort( char * base, int len )
- {
- int i, j;
-
- for (i = 0, j = len - 1; i < j; i++)
- {
- char curr, tmp;
-
- for (curr = i + 1, tmp = base[curr]; curr > 0 && tmp < base[curr-1]; curr--)
- base[curr] = base[curr - 1];
-
- base[curr] = tmp;
-
- }
- }
-
- /***********************************************************************\
- * *
- * name: make_list_node *
- * *
- * descr: Initialize a list_node. *
- * *
- \***********************************************************************/
-
- /* Initializes a List_Node. This requires obtaining memory for the KEY_SET
- and UNIQ_SET, initializing them using the information stored in the
- KEY_POSITIONS array in Options, and checking for simple errors.
- It's important to note that KEY and REST are both pointers to
- the different offsets into the same block of dynamic memory pointed to
- by parameter K. The data member REST is used to store any additional fields
- of the input file (it is set to the "" string if Option[TYPE] is not enabled).
- This is useful if the user wishes to incorporate a lookup structure,
- rather than just an array of keys. */
-
- LIST_NODE * make_list_node( char * k, int len )
- {
- LIST_NODE *temp = (LIST_NODE *) xmalloc (sizeof (LIST_NODE));
- int positions = 1 + (OPTION_ENABLED (option, ALLCHARS) ? len : TOTAL_POSITIONS (option) + 1);
- char *ptr, *ptr1, *ptr2;
-
- temp->key_set = (char *) xmalloc (positions + positions); /* Save 1 call to new. */
- temp->uniq_set = temp->key_set + positions;
- ptr = temp->key_set;
- k[len] = '\0'; /* Null terminate KEY to separate it from REST. */
- temp->key = k;
- temp->next = 0;
- temp->index = 0;
- temp->length = len;
- temp->link = 0;
- temp->rest = OPTION_ENABLED (option, TYPE) ? k + len + 1 : "";
-
- if (OPTION_ENABLED (option, ALLCHARS)) /* Use all the character position in the KEY. */
-
- for (; *k; k++, ptr++)
- ++occurrences[*ptr = *k];
-
- else /* Only use those character positions specified by the user. */
- {
- int i;
-
- /* Iterate thru the list of key_positions, initializing occurrences table
- and temp->key_set (via char * pointer ptr). */
-
- for(RESET (option); (i = GET (option)) != EOS; )
- {
- if (i == WORD_END) /* Special notation for last KEY position, i.e. '$'. */
- *ptr = temp->key[len - 1];
- else if (i <= len) /* Within range of KEY length, so we'll keep it. */
- *ptr = temp->key[i - 1];
- else /* Out of range of KEY length, so we'll just skip it. */
- continue;
- ++occurrences[*ptr++];
- }
-
- if (ptr == temp->key_set) /* Didn't get any hits, i.e., no usable positions. */
- report_error ("can't hash keyword %s with chosen key positions\n%a", temp->key);
- }
-
- *ptr = '\0'; /* Terminate this bastard.... */
- set_sort (temp->key_set, ptr - temp->key_set); /* Sort the KEY_SET items alphabetically. */
-
- /* Eliminate UNIQ_SET duplicates, this saves time later on.... */
-
- for (ptr1 = temp->key_set, ptr2 = temp->uniq_set; *ptr1; ptr1++)
- if (*ptr1 != ptr1[1])
- *ptr2++ = *ptr1;
-
- *ptr2 = '\0'; /* NULL terminate the UNIQ_SET. */
- return temp;
- }
-